package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.bean.DishFlavor;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 为修改页面回显数据
     * @param id 修改的菜品的id
     * @return 因为要显示口味详情  所以使用封装了flavors字段的Dto
     */
    @GetMapping("/{id}")//路径传参
    public R<DishDto> getMess(@PathVariable long id){
        DishDto dishDto=dishService.getMess(id);
        return R.success(dishDto);
    }

    /**
     * 添加菜品，在持久层需要同时操作dish和dish_flavor两张表
     * @param dishDto 传入的数据有口味详情  所以使用封装了flavors字段的Dto
     * @return "成功信息"
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto){
        //dish和dishFlavor表都需要插入
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    /**
     * 修改菜品，在持久层需要同时操作dish和dish_flavor两张表
     * @param dishDto 传入的数据有口味详情  所以使用封装了flavors字段的Dto
     * @return "成功信息"
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        //dish和dishFlavor表都需要改变
        dishService.updateWithFlavro(dishDto);
        return R.success("修改成功");
    }

    /**
     * 分页  version:1 弃用了
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    public R<Page<Dish>> page(int page,int pageSize,@RequestParam(required = false)String name){
        //获取Page对象
        Page<Dish> dishPage = new Page<>(page, pageSize);
        //封装构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Dish::getUpdateTime)
                .like(name!=null,Dish::getName,name);
        //分页查询
        return dishService.pageR(dishPage,wrapper);
    }

    /**
     * 分页 version:2
     * Dish只有菜品分类的数字，要转换成category_name  所以要使用DishDto对象，封装了字段category_name
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R pageR(int page,int pageSize,String name){
        //因为只有dish表，没有dishDto表，所以还是要查询dish表
        Page<Dish> dishPage = new Page<Dish>(page,pageSize);

        //封装构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Dish::getUpdateTime)
                .like(name!=null,Dish::getName,name);

        //要交给service查询后再做 dish->dishDto 转换
        return dishService.showDishDto(dishPage,wrapper);
    }

    //改变菜品状态
    @PostMapping("/status/0")
    public R changeStatus0(@RequestParam List<Long> ids){
        List<Dish> dishes = dishMapper.selectBatchIds(ids);
        for (Dish dish :dishes) {
            dish.setStatus(0);
        }

        dishService.updateBatchById(dishes);
        return R.success("已停售");
    }

    //改变菜品状态
    @PostMapping("/status/1")
    public R changeStatus1(@RequestParam List<Long> ids){
        List<Dish> dishes = dishMapper.selectBatchIds(ids);
        for (Dish dish :dishes) {
            dish.setStatus(1);
        }

        dishService.updateBatchById(dishes);
        return R.success("已启售");
    }

    //删除菜品
    //一名多值,可以使用集合接收即可!但是需要使用@RequestParam注解指定
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        dishService.removeBatchByIds(ids);
        //口味不用跟着一起删
        /*for (Long id :ids) {
            dishFlavorMapper.
                    delete(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,id));
        }*/
        return R.success("删除成功");
    }

    //为新增套餐返回菜品
    @GetMapping("/list")
    public R list(Long categoryId,String name){
        List<Dish> dishes = dishMapper.
                selectList(new LambdaQueryWrapper<Dish>().eq(categoryId!=null,
                        Dish::getCategoryId, categoryId).like(name!=null,Dish::getName,name));
        return R.success(dishes);
    }
}
